import VueRouter from 'vue-router'

import About from '../pages/About'
import Home from '../pages/Home'
import Message from '../pages/Message'
import News from '../pages/News'
import Detail from '../pages/Detail'

const router = new VueRouter({
	mode: 'history',
	routes: [
		{
			name: 'zhuye',
			path: '/Home',
			component: Home,
			meta: { isAuth: false, title:'主页'},
			children: [
				{
					name: 'xiaoxi',
					path: 'Message',
					component: Message,
					meta: { isAuth: true, title:'消息'},
					children: [
						{
							name: 'gotoDt',
							// path: 'Detail',
							path: 'Detail/:id/:title',
							component: Detail,
							meta: { isAuth: false, title:'详情'},
							//props: {a:1, b:'hello'}
							//props: true
							props($route) {
								return {
									id: $route.params.id,
									title: $route.params.title
								}
							}
						}
					]
				},
				{
					name: 'xinwen',
					path: 'News',
					component: News,
					meta: { isAuth: false, title:'新闻'},
					beforeEnter(to, from, next) {
						console.log('news self beforeEnter ', to, from)
						next()
					}
				}
			]
		},
		{
			name: 'guanyu',
			path: '/About',
			component: About,
			meta: { isAuth: false, title:'关于'}
		}

	]
})

//全局前置路由守卫 初始化时被调用，每次切换之前被调用
router.beforeEach((to, from, next) => {
	console.log('beforeEach', to, from)
	//if(to.name === 'xinwen' || to.name === 'xiaoxi') {
	if(to.meta.isAuth) {
		if(localStorage.getItem('school') === 'fafi') {
			next()
		} else {
			alert('error')
		}
	} else {
		next()
	}
})

router.afterEach((to, from) => {
	console.log('afterEach', to, from)
	document.title = to.meta.title || '系统'
})
export default router